package com.getpebble.android.framework.comm;

import com.getpebble.android.bluetooth.protocol.ProtocolMessage;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.model.PblDevice;
import com.getpebble.android.framework.DeviceInterfaces.IDeviceMessageSender;
import com.getpebble.android.framework.protocol.outbound.PblOutboundMessage;
import java.nio.ByteBuffer;

public class MessageSender {
    private PblDevice mDevice;
    private IDeviceMessageSender mDeviceMessageSender;

    public MessageSender(IDeviceMessageSender deviceMessageSender, PblDevice device) {
        if (deviceMessageSender == null) {
            throw new IllegalArgumentException("'deviceMessageSender' cannot be null!");
        } else if (device == null) {
            throw new IllegalArgumentException("'device' cannot be null!");
        } else {
            this.mDeviceMessageSender = deviceMessageSender;
            this.mDevice = device;
        }
    }

    private IDeviceMessageSender getDeviceMessageSender() {
        return this.mDeviceMessageSender;
    }

    public PblDevice getDevice() {
        return this.mDevice;
    }

    public boolean sendMessage(PblOutboundMessage message) {
        Trace.debug("MessageSender", "Sending message of type: " + message.getClass().getSimpleName());
        return sendMessage(ProtocolMessage.from(ByteBuffer.wrap(message.getBytes())));
    }

    protected boolean sendMessage(ProtocolMessage message) {
        return getDeviceMessageSender().sendMessage(getDevice(), message);
    }
}
